<?php

  session_start();
  include "config.php";
  include "ALmailingList.php";

  if(isset($_SESSION["user"]))
  {
    include "$header";
    include "$body";
?>

<div style="position:absolute; top:0px; left:1px; border:0;">
  <span class="page">Catalogue Mailing List</span>
</div>
  
<?php

    include "$H_ml";

	if(isset($_POST["submit"]))
    {
	  $title = ltrim($_POST["title"]);
      $surname = ltrim($_POST["surname"]);
      $given_name = ltrim($_POST["given_name"]);
      $address = ltrim($_POST["address"]);
      $pcode = ltrim($_POST["pcode"]);
	  if($title == "Select:")
      {
        $title_no = 0;
      }
	  else if($title == "Miss")
      {
        $title_no = 1;
      }
      else if($title == "Mr")
      {
        $title_no = 2;
      }
      else if($title == "Mrs")
      {
        $title_no = 3;
      }
      else if($title == "Ms")
      {
        $title_no = 4;
      }
	  if(validateDetails($title, $surname, $given_name, $address, $pcode) == false)
      {
        showForm();
        echo "<SCRIPT language='Javascript'>\n";
        echo "  mailingList.title.options.selectedIndex=" . $title_no . ";\n";
        echo "  mailingList.surname.value='" . $surname . "';\n";
        echo "  mailingList.given_name.value='" . $given_name . "';\n";
        echo "  mailingList.address.value='" . $address . "';\n";
        echo "  mailingList.pcode.value='" . $pcode . "';\n";
        echo "</SCRIPT>";
      }
	  else
      {
        $_SESSION["ml_title"] = $title;
        $_SESSION["ml_surname"] = $surname;
        $_SESSION["ml_given_name"] = $given_name;
        $_SESSION["ml_address"] = $address;
        $_SESSION["ml_pcode"] = $pcode;
        echo "<SCRIPT language='Javascript'>";
        echo "  window.location.href = 'mailingList_1confirmDetails.php';";
        echo "</SCRIPT>";
      }
    }
	else if (isset($_SESSION["ml_title"]))
    {
      unset($_SESSION["ml_title"]);
      unset($_SESSION["ml_surname"]);
      unset($_SESSION["ml_given_name"]);
      unset($_SESSION["ml_address"]);
      unset($_SESSION["ml_pcode"]);
      showForm();
      echo "<SCRIPT language='Javascript'>";
      echo "  mailingList.title.selectedIndex=0;";
      echo "  mailingList.surname.value='';";
      echo "  mailingList.given_name.value='';";
      echo "  mailingList.address.value='';";
      echo "  mailingList.pcode.value='';";
      echo "</SCRIPT>";
    }
	else if (isset($_SESSION["c_surname"]))
    {
      showForm();
      echo "<SCRIPT language='Javascript'>";
      echo "  mailingList.title.options.selectedIndex=" . ($_SESSION["c_title"] + 1) . ";";
      echo "  mailingList.surname.value='" . $_SESSION["c_surname"] . "';";
      echo "  mailingList.given_name.value='" . $_SESSION["c_given_name"] . "';";
      echo "  mailingList.address.value='" . $_SESSION["c_address"] . "';";
      echo "  mailingList.pcode.value='" . $_SESSION["c_pcode"] . "';";
      echo "</SCRIPT>";
    }
	else
    {
      showForm();
    }
    include "$footer";
  }
  else
  {
    session_destroy();
    echo "<SCRIPT language='Javascript'>";
    echo "  parent.window.location.href = 'index.php';";
    echo "</SCRIPT>";
  }

  function showForm()
  {

?>

Please enter the name and postal address of the customer to receive the catalogue.
<p>
<font color="red">*</font> = mandatory field
<p>

<FORM name="mailingList" action="" method="post">
<TABLE class="mailingList" cellspacing="5" cellpadding="5">
  <TR>
    <TD class="mlField">
	  Title<font color="red">*</font>
	</TD>
    <TD class="mlDetails">
	  <select class="mailingList" name="title" style="width:70px;">
        <option value="Select:">Select:</option>
	    <option value="Miss">Miss</option>
		<option value="Mr">Mr</option>
		<option value="Mrs">Mrs</option>
		<option value="Ms">Ms</option>
	  </select>
	</TD>
  </TR>
  <TR>
    <TD class="mlField">
	  Surname<font color="red">*</font>
	</TD>
    <TD class="mlDetails">
	  <input type="text" class="mailingList" name="surname" style="width:150px;" />
	</TD>
  </TR>
  <TR>
    <TD class="mlField">
	  Given Name<font color="red">*</font>
	</TD>
	<TD class="mlDetails">
	  <input type="text" class="mailingList" name="given_name" style="width:150px" />
	</TD>
  </TR>
  <TR>
    <TD class="mlField">
	  Address<font color="red">*</font>
	</TD>
    <TD class="mlDetails">
	  <input type="text" class="mailingList" name="address" style="width:150px" />
	</TD>
  </TR>
  <TR>
    <TD class="mlField">
	  Post Code<font color="red">*</font>
	</TD>
    <TD class="mlDetails">
	  <input type="text" class="mailingList" name="pcode" style="width:50px" />
	</TD>
  </TR>
</TABLE>

<p>

<input class="submit_off" type="reset" value="Reset" name="reset" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />
&nbsp;&nbsp;
<input class="submit_off" type="submit" value="Submit" name="submit" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />

</FORM>

</CENTER>

</BODY>

<?php

  }

?>